<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/blog/","images":"/blog/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/blog/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="假如有个表commodity有个字段add_time,它的数据类型为datetime,有人可能会这样写sql： 1select * from product where add_time &#x3D; &amp;#x27;2013-01-12&amp;#x27;;  这种语句，如果你存储的格式是YY-mm-dd这样，那么OK，如果你存储的格式是：2018-01-12 23:23:56 这种就悲剧了，此时你可以用 DATE(">
<meta property="og:type" content="article">
<meta property="og:title" content="Mysql中日期比较大小的方法">
<meta property="og:url" content="http://littlefxc.github.io/2019/10/08/Mysql%E4%B8%AD%E6%97%A5%E6%9C%9F%E6%AF%94%E8%BE%83%E5%A4%A7%E5%B0%8F%E7%9A%84%E6%96%B9%E6%B3%95/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="假如有个表commodity有个字段add_time,它的数据类型为datetime,有人可能会这样写sql： 1select * from product where add_time &#x3D; &amp;#x27;2013-01-12&amp;#x27;;  这种语句，如果你存储的格式是YY-mm-dd这样，那么OK，如果你存储的格式是：2018-01-12 23:23:56 这种就悲剧了，此时你可以用 DATE(">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-10-08T10:18:15.000Z">
<meta property="article:modified_time" content="2021-03-25T13:15:48.977Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="mysql">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://littlefxc.github.io/2019/10/08/Mysql%E4%B8%AD%E6%97%A5%E6%9C%9F%E6%AF%94%E8%BE%83%E5%A4%A7%E5%B0%8F%E7%9A%84%E6%96%B9%E6%B3%95/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>Mysql中日期比较大小的方法 | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/blog/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/blog/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/blog/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/blog/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/blog/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">184</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">35</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">115</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/10/08/Mysql%E4%B8%AD%E6%97%A5%E6%9C%9F%E6%AF%94%E8%BE%83%E5%A4%A7%E5%B0%8F%E7%9A%84%E6%96%B9%E6%B3%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Mysql中日期比较大小的方法
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-10-08 18:18:15" itemprop="dateCreated datePublished" datetime="2019-10-08T18:18:15+08:00">2019-10-08</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/blog/categories/RDBMS/" itemprop="url" rel="index"><span itemprop="name">RDBMS</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>假如有个表<code>commodity</code>有个字段add_time,它的数据类型为datetime,有人可能会这样写sql：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> product <span class="keyword">where</span> add_time <span class="operator">=</span> <span class="string">&#x27;2013-01-12&#x27;</span>;</span><br></pre></td></tr></table></figure>

<p>这种语句，如果你存储的格式是YY-mm-dd这样，那么OK，<br>如果你存储的格式是：2018-01-12 23:23:56 这种就悲剧了，此时你可以用 DATE() 函数用来返回日期的部分；sql如下处理：</p>
  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> product <span class="keyword">where</span> <span class="type">Date</span>(add_time) <span class="operator">=</span> <span class="string">&#x27;2018-01-12&#x27;</span></span><br></pre></td></tr></table></figure>

<ol>
<li>如果你要查询2017年1月份加入的产品呢？</li>
</ol>
  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> product <span class="keyword">where</span> <span class="type">date</span>(add_time) <span class="keyword">between</span> <span class="string">&#x27;2013-01-01&#x27;</span> <span class="keyword">and</span> <span class="string">&#x27;2013-01-31&#x27;</span></span><br></pre></td></tr></table></figure>

<p>  或者： 还可以这样写：</p>
  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> product <span class="keyword">where</span> <span class="keyword">Year</span>(add_time) <span class="operator">=</span> <span class="number">2013</span> <span class="keyword">and</span> <span class="keyword">Month</span>(add_time) <span class="operator">=</span> <span class="number">1</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>其date_col的值是在最后30天以内：</li>
</ol>
  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> commodity  <span class="keyword">WHERE</span> TO_DAYS( NOW() ) <span class="operator">-</span> TO_DAYS( date_col )  <span class="operator">&lt;=</span> <span class="number">30</span>;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>DAYOFWEEK(date) ： 返回日期date的星期索引(1=星期天, 7=星期六) 这些索引值对应于ODBC标准。</li>
</ol>
  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> DAYOFWEEK(<span class="string">&#x27;1998-02-03&#x27;</span>);                     <span class="operator">-</span><span class="operator">&gt;</span>  <span class="number">3</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li>WEEKDAY(date)  ： 返回date的星期索引 (0=星期一, 6= 星期天)</li>
</ol>
  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> WEEKDAY(<span class="string">&#x27;1997-10-04 22:23:00&#x27;</span>);             <span class="operator">-</span><span class="operator">&gt;</span> <span class="number">5</span></span><br><span class="line"><span class="keyword">select</span> WEEKDAY(<span class="string">&#x27;2018-07-07&#x27;</span>);                      <span class="operator">-</span><span class="operator">&gt;</span> <span class="number">5</span></span><br></pre></td></tr></table></figure>

<ol start="5">
<li>DAYOFMONTH(date) ：  返回date的月份中日期，在1到31范围内。</li>
</ol>
  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> DAYOFMONTH(<span class="string">&#x27;1998-02-03&#x27;</span>);                    <span class="operator">-</span><span class="operator">&gt;</span> <span class="number">3</span></span><br></pre></td></tr></table></figure>

<ol start="6">
<li>DAYOFYEAR(date) ：  返回date在一年中的日数, 在1到366范围内。</li>
</ol>
  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> DAYOFYEAR(<span class="string">&#x27;1998-02-03&#x27;</span>);                     <span class="operator">-</span><span class="operator">&gt;</span> <span class="number">34</span></span><br></pre></td></tr></table></figure>

<ol start="7">
<li>MONTH(date) ：   返回date的月份，范围1到12。</li>
</ol>
  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">MONTH</span>(<span class="string">&#x27;1998-02-03&#x27;</span>);                         <span class="operator">-</span><span class="operator">&gt;</span> <span class="number">2</span></span><br></pre></td></tr></table></figure>

<ol start="8">
<li>DAYNAME(date) ： 返回date的星期名字。</li>
</ol>
  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> DAYNAME(&quot;1998-02-05&quot;);                       <span class="operator">-</span><span class="operator">&gt;</span> <span class="string">&#x27;Thursday&#x27;</span></span><br><span class="line"><span class="keyword">select</span> DAYNAME(&quot;20180707&quot;);                         <span class="operator">-</span><span class="operator">&gt;</span> <span class="string">&#x27;Saturday&#x27;</span></span><br></pre></td></tr></table></figure>

<ol>
<li>MONTHNAME(date) ： 返回date的月份名字。</li>
</ol>
  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> MONTHNAME(&quot;1998-02-05&quot;);                     <span class="operator">-</span><span class="operator">&gt;</span> <span class="string">&#x27;February&#x27;</span></span><br></pre></td></tr></table></figure>

<ol start="10">
<li>QUARTER(date)  ： 返回date一年中的季度，范围1到4。</li>
</ol>
  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> QUARTER(<span class="string">&#x27;98-04-01&#x27;</span>);                          <span class="operator">-</span><span class="operator">&gt;</span> <span class="number">2</span></span><br><span class="line"><span class="keyword">select</span> QUARTER(<span class="string">&#x27;20180707&#x27;</span>);                          <span class="operator">-</span><span class="operator">&gt;</span> <span class="number">3</span></span><br></pre></td></tr></table></figure>

<p>这是做统计数据时候用了的sql.</p>
<p>查询今天、昨天、一周内、8周、12周等数据 直接在sql写时间查询。 避免时区转化。数据库我们使用的UTC时间。</p>
<ul>
<li><p>Today</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> str_to_date(curdate(),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>) <span class="keyword">as</span> todayBegin;</span><br><span class="line"><span class="keyword">SELECT</span> date_add(date_add(str_to_date(curdate(),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>),<span class="type">interval</span> <span class="number">1</span> <span class="keyword">DAY</span>),<span class="type">INTERVAL</span> <span class="number">-1</span> <span class="keyword">SECOND</span>) <span class="keyword">as</span> todayEnd;</span><br></pre></td></tr></table></figure></li>
<li><p>昨天</p>
<p>``sql<br>SELECT str_to_date(date_sub(curdate(), interval 1 day),’%Y-%m-%d %H:%i:%s’) as yestodayBegin;<br>SELECT date_add(date_add(str_to_date(date_sub(curdate(), interval 1 day),’%Y-%m-%d %H:%i:%s’),interval 1 DAY),INTERVAL -1 SECOND) as yestodayEnd;</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 最近7天 Last Seven days</span><br><span class="line"></span><br><span class="line">  &#96;&#96;&#96;sql</span><br><span class="line">  SELECT str_to_date(date_sub(curdate(), interval 6 day),&#39;%Y-%m-%d %H:%i:%s&#39;) as lastSevenDaysBegin;</span><br><span class="line">  SELECT date_add(date_add(str_to_date(curdate(),&#39;%Y-%m-%d %H:%i:%s&#39;),interval 1 DAY),INTERVAL -1 SECOND) as todayEnd;</span><br></pre></td></tr></table></figure>
</li>
<li><p>最近14天 Last Fourteen days</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> str_to_date(date_sub(curdate(), <span class="type">interval</span> <span class="number">13</span> <span class="keyword">day</span>),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>) <span class="keyword">as</span> lastFourteenDaysBegin;</span><br><span class="line"><span class="keyword">SELECT</span> date_add(date_add(str_to_date(curdate(),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>),<span class="type">interval</span> <span class="number">1</span> <span class="keyword">DAY</span>),<span class="type">INTERVAL</span> <span class="number">-1</span> <span class="keyword">SECOND</span>) <span class="keyword">as</span> todayEnd;</span><br></pre></td></tr></table></figure></li>
<li><p>最近30天 Last Thirty days</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> str_to_date(date_sub(curdate(), <span class="type">interval</span> <span class="number">29</span> <span class="keyword">day</span>),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>) <span class="keyword">as</span> lastThirtyDaysBegin;</span><br><span class="line"><span class="keyword">SELECT</span> date_add(date_add(str_to_date(curdate(),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>),<span class="type">interval</span> <span class="number">1</span> <span class="keyword">DAY</span>),<span class="type">INTERVAL</span> <span class="number">-1</span> <span class="keyword">SECOND</span>) <span class="keyword">as</span> todayEnd;</span><br></pre></td></tr></table></figure></li>
<li><p>最近8周 Last eight weeks</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> str_to_date(date_sub(curdate(), <span class="type">interval</span> <span class="number">8</span> week),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>) <span class="keyword">as</span> lastEightWeeksBegin;</span><br><span class="line"><span class="keyword">SELECT</span> date_add(date_add(str_to_date(curdate(),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>),<span class="type">interval</span> <span class="number">1</span> <span class="keyword">DAY</span>),<span class="type">INTERVAL</span> <span class="number">-1</span> <span class="keyword">SECOND</span>) <span class="keyword">as</span> todayEnd;</span><br></pre></td></tr></table></figure></li>
<li><p>12 最近12周 Last twelve weeks</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> str_to_date(date_sub(curdate(), <span class="type">interval</span> <span class="number">12</span> week),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>) <span class="keyword">as</span> lastTwelveWeeksBegin;</span><br><span class="line"><span class="keyword">SELECT</span> date_add(date_add(str_to_date(curdate(),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>),<span class="type">interval</span> <span class="number">1</span> <span class="keyword">DAY</span>),<span class="type">INTERVAL</span> <span class="number">-1</span> <span class="keyword">SECOND</span>) <span class="keyword">as</span> todayEnd;</span><br></pre></td></tr></table></figure></li>
<li><p>最近3月 Last three month</p>
<p>``sql<br>SELECT str_to_date(date_sub(curdate(), interval 3 month),’%Y-%m-%d %H:%i:%s’) as lastThreeMonthBegin;</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 未来3天 Three day later</span><br><span class="line"></span><br><span class="line">  &#96;&#96;&#96;sql</span><br><span class="line">  SELECT str_to_date(date_sub(curdate(), interval -3 day),&#39;%Y-%m-%d %H:%i:%s&#39;) as threeDaysLaterEnd;</span><br></pre></td></tr></table></figure></li>
<li><p>未来3月 Three months later</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> str_to_date(date_sub(curdate(), <span class="type">interval</span> <span class="number">-3</span> <span class="keyword">month</span>),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>) <span class="keyword">as</span> threeMonthsLaterEnd; </span><br></pre></td></tr></table></figure></li>
<li><p>取一天的开始时间</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> str_to_date(DATE_FORMAT(<span class="string">&#x27;2018-03-03&#x27;</span>,<span class="string">&#x27;%Y-%m-%d&#x27;</span>),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>);</span><br></pre></td></tr></table></figure></li>
<li><p>取第二天的开始时间</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> DATE_ADD(str_to_date(DATE_FORMAT(NOW(),<span class="string">&#x27;%Y-%m-%d&#x27;</span>),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>),<span class="type">INTERVAL</span> <span class="number">1</span> <span class="keyword">DAY</span>);</span><br><span class="line"><span class="keyword">select</span> DATE_ADD(str_to_date(DATE_FORMAT(<span class="string">&#x27;2018-03-03&#x27;</span>,<span class="string">&#x27;%Y-%m-%d&#x27;</span>),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>),<span class="type">INTERVAL</span> <span class="number">1</span> <span class="keyword">DAY</span>);</span><br></pre></td></tr></table></figure></li>
<li><p>取一天的结束时间</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> DATE_ADD(DATE_ADD(str_to_date(DATE_FORMAT(NOW(),<span class="string">&#x27;%Y-%m-%d&#x27;</span>),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>),<span class="type">INTERVAL</span> <span class="number">1</span> <span class="keyword">DAY</span>),<span class="type">INTERVAL</span> <span class="number">-1</span> <span class="keyword">SECOND</span>);</span><br><span class="line"><span class="keyword">select</span> DATE_ADD(DATE_ADD(str_to_date(DATE_FORMAT(<span class="string">&#x27;2018-03-03&#x27;</span>,<span class="string">&#x27;%Y-%m-%d&#x27;</span>),<span class="string">&#x27;%Y-%m-%d %H:%i:%s&#x27;</span>),<span class="type">INTERVAL</span> <span class="number">1</span> <span class="keyword">DAY</span>),<span class="type">INTERVAL</span> <span class="number">-1</span> <span class="keyword">SECOND</span>);</span><br></pre></td></tr></table></figure></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/mysql/" rel="tag"># mysql</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/blog/2019/09/12/springboot%E4%BD%BF%E7%94%A8rabbitmq/" rel="prev" title="springboot使用rabbitmq">
                  <i class="fa fa-chevron-left"></i> springboot使用rabbitmq
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/blog/2019/11/27/cron%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%B8%B8%E7%94%A8%E4%BE%8B%E5%AD%90/" rel="next" title="cron表达式常用例子">
                  cron表达式常用例子 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/blog/js/utils.js"></script><script src="/blog/js/motion.js"></script><script src="/blog/js/schemes/muse.js"></script><script src="/blog/js/next-boot.js"></script>

  
<script src="/blog/js/local-search.js"></script>






  





</body>
</html>
